import Mock from 'mockjs'
// 设置响应有200-300的延时，模拟正常的网络请求
Mock.setup({
  timeout: '200-600',
})
Mock.mock('https://www.demo.com/login', 'post', (request: any) => {
  const { username, password } = JSON.parse(request.body)
  if (username === 'admin' && password === 'abc123456') {
    return {
      code: 200,
      msg: 'success',
      data: {
        id: 13324,
        username: '张三',
        token: 'mocktokenabc123456',
      },
    }
  } else {
    return {
      code: 400,
      msg: '账号或密码错误',
    }
  }
})

Mock.mock('https://www.demo.com/menu', 'get', () => {
  return {
    code: 200,
    msg: 'success',
    data: menuList,
  }
})

const menuList = [
  {
    icon: 'DashboardOutlined',
    label: '工作台',
    key: '/dashboard',
  },
  {
    icon: 'TeamOutlined',
    label: '租户管理',
    key: '/users',
    children: [
      {
        icon: 'UnorderedListOutlined',
        label: '租户列表',
        key: '/users/list',
      },
    ],
  },
  {
    icon: 'LaptopOutlined',
    label: '物业管理',
    key: '/estate',
    children: [
      {
        icon: 'InsertRowLeftOutlined',
        label: '楼宇管理',
        key: '/estate/tenement',
      },
      {
        icon: 'BankOutlined',
        label: '房间管理',
        key: '/estate/room',
      },
      {
        icon: 'TruckOutlined',
        label: '车辆信息',
        key: '/estate/car',
      },
    ],
  },
  {
    icon: 'ToolOutlined',
    label: '报修管理',
    key: '/repair',
  },
  {
    icon: 'DollarOutlined',
    label: '财务管理',
    key: '/finance',
    children: [
      {
        icon: 'ProfileOutlined',
        label: '合同管理',
        key: '/finance/contract',
      },
      // {
      //   icon: 'FrownOutlined',
      //   label: '合同详情',
      //   key: '/finance/surrender',
      // },
      {
        icon: 'FileTextOutlined',
        label: '账单管理',
        key: '/finance/bill',
      },
    ],
  },
  {
    icon: 'TransactionOutlined',
    label: '招商管理',
    key: '/merchants',
  },
  {
    icon: 'FundProjectionScreenOutlined',
    label: '运营管理',
    key: '/operation',
    children: [
      {
        icon: 'FundViewOutlined',
        label: '运营总览',
        key: '/operation/all',
      },
      {
        icon: 'ReadOutlined',
        label: '文章发布',
        key: '/operation/article',
      },
      {
        icon: 'CommentOutlined',
        label: '内容评论',
        key: '/operation/comments',
      },
    ],
  },
  {
    icon: 'ToolOutlined',
    label: '设备管理',
    key: '/equipment',
  },
  {
    icon: 'ThunderboltOutlined',
    label: '能源消耗',
    key: '/energy',
  },
]

Mock.mock('https://www.demo.com/energyData', 'get', () => {
  return {
    code: 200,
    message: '请求成功',
    data: [
      { name: '煤', data: [120, 132, 101, 134, 90, 230, 210] },
      { name: '气', data: [220, 182, 191, 234, 290, 330, 310] },
      { name: '油', data: [150, 232, 201, 154, 190, 330, 410] },
      { name: '电', data: [320, 332, 301, 334, 390, 330, 320] },
      { name: '热', data: [820, 932, 901, 934, 1290, 1330, 1320] },
    ],
  }
})

Mock.Random.extend({
  phone: function () {
    var phonePrefixs = ['13', '14', '15', '16', '17', '18', '19'] // 自己写前缀哈
    return this.pick(phonePrefixs) + Mock.mock(/\d{9}/) //Number()
  },
})

Mock.mock('https://www.demo.com/userList', 'post', (options: any) => {
  const { pageSize } = JSON.parse(options.body)
  return {
    code: 200,
    message: '请求成功',
    data: Mock.mock({
      [`list|${pageSize}`]: [
        {
          id: "@string('number',6)", //随机生成一个六位数字id
          name: '@cname', //随机生成一个人名
          'status|1': [1, 2, 3],
          tel: '@phone',
          'business|1': ['制造业', '互联网', '新媒体', '美业', '新能源', '物流', '电商'],
          email: '@email',
          creditCode: "@string('number',18)",
          industryNum: "@string('number',15)",
          organizationCode: "@string('upper',9)",
          legalPerson: '@cname',
        },
      ],
      total: 78,
    }),
  }
})

Mock.mock(RegExp('https://www\.demo\.com/deleteById/.*'), 'delete', (options: any) => {
  const match = options.url.match(/\/deleteById\/([^/?]+)/)
  console.log(match?.[1])
  return {
    code: 200,
    message: '删除成功',
  }
})

Mock.mock(RegExp('https://www.demo.com/editUserData'), 'post', (options: any) => {
  console.log(options.body)
  return {
    code: 200,
    message: '删除成功',
  }
})

Mock.mock(RegExp('https://www.demo.com/tenement/list'), 'get', () => {
  return {
    code: 200,
    message: '删除成功',
    data: {
      list: [
        {
          key: '1',
          name: 'A1幢写字楼',
          person: '王达',
          tel: '16654789654',
          status: '建设中',
          vacancyRate: 60,
          propertyFee: '3.5%',
        },
        {
          key: '2',
          name: 'A2幢写字楼',
          person: '苏乐凯',
          tel: '13698756669',
          status: '已竣工',
          vacancyRate: 40,
          propertyFee: '3.8%',
        },
        {
          key: '3',
          name: 'B1幢写字楼',
          person: '莉亚',
          tel: '15587966698',
          status: '使用中',
          vacancyRate: 20,
          propertyFee: '3.1%',
        },
        {
          key: '4',
          name: 'B2幢写字楼',
          person: '常可',
          tel: '13698756324',
          status: '使用中',
          vacancyRate: 30,
          propertyFee: '4.0%',
        },
        {
          key: '5',
          name: 'C1幢写字楼',
          person: '刘伟',
          tel: '19878965444',
          status: '使用中',
          vacancyRate: 50,
          propertyFee: '3.5%',
        },
        {
          key: '6',
          name: 'C2幢写字楼',
          person: '孙强浩',
          tel: '13369888562',
          status: '使用中',
          vacancyRate: 10,
          propertyFee: '2.9%',
        },
        {
          key: '7',
          name: '天汇国际大厦A座',
          person: '马浩瀚',
          tel: '13578549687',
          status: '使用中',
          vacancyRate: 25,
          propertyFee: '3.7%',
        },
        {
          key: '8',
          name: '时代金融广场',
          person: '杨柳',
          tel: '18745889874',
          status: '使用中',
          vacancyRate: 15,
          propertyFee: '3.3%',
        },
        {
          key: '9',
          name: '时代金融广场',
          person: '杨柳',
          tel: '18745889874',
          status: '使用中',
          vacancyRate: 15,
          propertyFee: '3.3%',
        },
        {
          key: '10',
          name: '时代金融广场',
          person: '杨柳',
          tel: '18745889874',
          status: '使用中',
          vacancyRate: 15,
          propertyFee: '3.3%',
        },
      ],
      total: 19,
    },
  }
})

//获取房间列表的接口
function generateRooms() {
  const rooms = []
  for (let i = 0; i < 50; i++) {
    const floor = 1 + Math.floor(i / 6)
    const roomNumber = floor * 100 + (101 + (i % 6))
    rooms.push({
      id: i + 1, // 新增递增ID（从1开始）
      roomNumber,
      decorationType: Mock.Random.pick(['毛坯', '精装']),
      area: Mock.Random.integer(70, 300),
      unitPrice: Mock.Random.integer(1, 3),
      src: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
    })
  }
  return rooms
}

Mock.mock('https://www.demo.com/roomList', 'post', () => {
  return {
    code: 200,
    message: '成功',
    data: {
      rooms: generateRooms(),
    },
  }
})

//合同管理
Mock.mock('https://www.demo.com/contractList', 'post', (options: any) => {
  const { pageSize } = JSON.parse(options.body)
  console.log('后端合同管理接到参数', JSON.parse(options.body))
  return {
    code: 200,
    message: '成功',
    data: Mock.mock({
      [`list|${pageSize}`]: [
        {
          id: '@string("number", 6)',
          contractNo: '@string("number", 6)',
          'type|1': ['租赁合同', '自定义合同', '购买合同'],
          'name|1': ['房屋租赁合同通用模版', '车位租赁合同通用模版', '商业房产买卖合同'],
          'startDate|1': ['2023-01-01', '2023-03-05', '2023-04-01'],
          'endDate|1': ['2024-01-01', '2024-03-05', '2024-04-01'],
          'jia|1': ['万物科技有限公司', '大鱼网络科技', '六六信息技术有限公司'],
          yi: '天明物业有限公司',
          'status|1': ['1', '2', '3'],
        },
      ],
      total: 54,
    }),
    // 生成55条数据
  }
})

//账单管理
Mock.mock('https://www.demo.com/billList', 'post', (options: any) => {
  const { pageSize } = JSON.parse(options.body)
  console.log('后端账单管理接到参数', JSON.parse(options.body))
  return {
    code: 200,
    message: '成功',
    data: Mock.mock({
      [`list|${pageSize}`]: [
        {
          accountNo: '@string("number", 6)',
          'status|1': ['1', '2'],
          'roomNo|1': ['A1幢写字楼-201', 'B1幢写字楼-402', 'B2幢写字楼-701', 'C2幢写字楼-1601'],
          'carNo|1': ['B109', 'C227', 'C106', 'D158'],
          'tel|1': ['@phone'],
          'costName1|1': [1278.0, 2633.0, 3698.0],
          costName2: '200元/月',
          'costName3|1': ['25800/年', '19800/年'],
          startDate: '2023-01-01',
          endDate: '2024-01-01',
          preferential: 0.0,
          money: 26000.0,
          'pay|1': ['微信', '支付宝', '现金', '银行卡转账'],
        },
      ],
      total: 54,
    }),
    // 生成55条数据
  }
})

Mock.mock('https://www.demo.com/equipmentList', 'post', (options: any) => {
  const { pageSize } = JSON.parse(options.body)
  return {
    code: 200,
    message: '成功',
    data: Mock.mock({
      [`list|${pageSize}`]: [
        {
          'id|+1': 1,
          no: '@string("upper", 2)@integer(1000, 9999)',
          name: '@cword(3, 8)',
          person: '@cname',
          tel: /^1[3456789]\d{9}$/,
          time: '@date',
          rest: '@cparagraph(1)',
          'status|1': [1, 2, 3],
          last: '@date',
          'type|1': ['type1', 'type2', 'type3', 'type4'],
          'from|1': ['北京', '上海', '广州', '深圳', '杭州'],
        },
      ],
      total: 78,
    }),
  }
})
